package com.example.newapplication.Fragment;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.newapplication.Bean.ScanResult;
import com.example.newapplication.MainActivity;
import com.example.newapplication.R;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import static android.app.Activity.RESULT_OK;


public class ScanFragment extends Fragment {


    ConstraintLayout result;
    private TextView delete,update;
    private TextView text1,text2,text3,text4,text5,text6,text7,text8,text9;
    private ImageView img;
    private ScanResult scanResult;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scan, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        result = getActivity().findViewById(R.id.textView24);
        init();

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"更新成功",Toast.LENGTH_SHORT).show();
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();
            }
        });
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
            //请求权限
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        ZxingConfig config = new ZxingConfig();
        config.setPlayBeep(true);//是否播放扫描声音 默认为true
        config.setShake(true);//是否震动  默认为true
        config.setDecodeBarCode(true);//是否扫描条形码 默认为true
        config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
        config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
        config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
        config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        startActivityForResult(intent,111);
        onActivityResult(111,RESULT_OK,intent);



    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == 111 && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                Gson gson = new Gson();
                scanResult = gson.fromJson(content, ScanResult.class);
                if(scanResult != null) {
                    Log.i("扫描结果：", scanResult.getMachineImg());
                    Glide.with(getActivity()).load(scanResult.getMachineImg()).into(img);
                    Toast.makeText(getActivity(), "扫描结果：" + scanResult.getMachineName(), Toast.LENGTH_LONG).show();
                    text1.setText(scanResult.getMachineName());
                    text2.setText(scanResult.getMachineCode());
                    text3.setText(scanResult.getMachineType());
                    text4.setText("大学生活动中心");
                    text5.setText(String.valueOf(scanResult.getMoney()));
                    text6.setText(scanResult.getMachineStatus());
                    text7.setText("全国大学生服务外包创业基地");
                    text8.setText(String.valueOf(scanResult.getResidual()));
                    text9.setText(scanResult.getIsImportant());
                }
            }

        }
    }
    private void init(){

        update = getActivity().findViewById(R.id.textView25);
        delete = getActivity().findViewById(R.id.textView26);

        img = getActivity().findViewById(R.id.img);
        text1 = getActivity().findViewById(R.id.textView127);
        text2 = getActivity().findViewById(R.id.textView128);
        text3 = getActivity().findViewById(R.id.textView129);
        text4 = getActivity().findViewById(R.id.textView130);
        text5 = getActivity().findViewById(R.id.textView131);
        text6 = getActivity().findViewById(R.id.textView136);
        text7 = getActivity().findViewById(R.id.textView137);
        text8 = getActivity().findViewById(R.id.textView138);
        text9 = getActivity().findViewById(R.id.textView139);
    }
}